﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace GraphicLib
{
    public class SamplesInition
    {
        /// <summary>
        /// 重新初始化样本
        /// </summary>
        public static void InitSamples()
        {
            CleanSample();
            MergeSamplesData();
        }

        /// <summary>
        /// 学习新样本
        /// </summary>
        public static void LearnNewSamples()
        {
            MergeSamplesData();
        }

        /// <summary>
        /// 将新的样本数据合并到原样本数据中
        /// </summary>
        private static void MergeSamplesData()
        {
            //得到样本图像
            List<Bitmap> bitmaps = BitmapGetter.GetSampleBitmaps();
            //得到每个样本的灰度值数组
            //切割N次，比较每块灰度值的方差，得到最大值放入Sample类，作为比较标准
            foreach (Bitmap bitmap in bitmaps)
            {
                byte[] grayBytes = GrayConvertor.GetGrayBytes(bitmap);

                List<List<byte[]>> bytesListList = GraphicSplit.SplitGraphicBytes(grayBytes, ProductCheck.SPLIT_COUNT);
                //循环比较每次切割得到方差
                for (int i = 0; i < bytesListList.Count; i++)
                {
                    List<byte[]> bytesList = bytesListList[i];
                    List<double> doubleList = null;
                    if (Sample.variencesList.Count < i + 1)
                    {
                        doubleList = new List<double>();
                        Sample.variencesList.Add(doubleList);
                    }
                    else
                    {
                        doubleList = Sample.variencesList[i];
                    }
                    for (int j = 0; j < bytesList.Count; j++)
                    {
                        double newVariance = VarianceCalculator.GetVariance(bytesList[j]);
                        if (doubleList.Count < j + 1)
                        {
                            doubleList.Add(newVariance);
                        }
                        else if (newVariance > doubleList[j])
                        {
                            doubleList[j] = newVariance;
                        }
                    }
                }
                bitmap.Dispose();
            }

            SerializeSample();

            //将新样本移到初始化过的样本目录中
            FileUtils.MoveFiles(Sample.NEW_SAMPLE_DIR, Sample.INITIALIZED_SAMPLE_DIR);
        }

        /// <summary>
        /// 清空样本数据
        /// </summary>
        private static void CleanSample()
        {
            FileUtils.DeleteFiles(Sample.INITIALIZED_SAMPLE_DIR);
            File.Delete(Sample.SAMPLE_DATA_PATH);
            Sample.variencesList.Clear();
        }
        
        /// <summary>
        /// 序列化当前的样本数据
        /// </summary>
        private static void SerializeSample()
        {
            using (FileStream fs = new FileStream(Sample.SAMPLE_DATA_PATH, FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(fs, Sample.variencesList);
            }
        }

        /// <summary>
        /// 反序列化已存在的样本数据
        /// </summary>
        public static void DeserializeSample()
        {
            if (File.Exists(Sample.SAMPLE_DATA_PATH))
            {
                using (FileStream fs = new FileStream(Sample.SAMPLE_DATA_PATH, FileMode.Open))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    Sample.variencesList = (List<List<double>>)formatter.Deserialize(fs);
                }
            }
        }
    }
}
